package com.example.com.uiux4.formwidget;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;

public class KeyEventActivity extends Activity {
	
	int x=100, y=100;
	
	protected class MyView extends View{

		public MyView(Context context) {
			super(context);
			setBackgroundColor(Color.YELLOW);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			canvas.drawRect(x, y, x+50, y+50, paint);
		}		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_key_event);
		MyView w = new MyView(this);
		w.setFocusable(true);
		w.setFocusableInTouchMode(true);
		setContentView(w);
		w.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(event.getAction() == KeyEvent.ACTION_UP){
					switch (keyCode){
					case KeyEvent.KEYCODE_J:
						x -= 30;
						break;
					case KeyEvent.KEYCODE_K:
						x += 30;
						break;
					}
					v.invalidate();
				}
				return false;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.key_event, menu);
		return true;
	}
}